#!/usr/bin/python
import sys, os #--
import pygst #--
pygst.require("0.10") #--
import gst #--
import pygtk #--
pygtk.require("2.0") #--
import gtk, gtk.glade #--
#--
#--
#=00
class Live:
    def __init__(self):
        gladeFile = os.path.join(os.path.dirname(__file__,), 'live.glade') #--
        self.xml = gtk.glade.XML(gladeFile, None) #--
        self.xml.signal_autoconnect(self) #--
        window = self.xml.get_widget("main") #--
        window.connect('delete-event', gtk.main_quit) #--
        window.show_all() #--
        self.pipeline = gst.Pipeline()
        gstElemMake = gst.element_factory_make
#=01 alsasrc
        mikesrc = gstElemMake("alsasrc")  #01
        mikesrc.set_property('device', 'hw:2') #01
        tee = gstElemMake("tee", "t") 
        queue0 = gstElemMake("queue") 
        audioconvert0 = gstElemMake("audioconvert") 
        self.pitch = gstElemMake("ladspa-amPitchshift") 
        self.pitch.set_property("Pitch-shift", 1)
        self.chorus = gstElemMake("ladspa-multivoiceChorus")
        self.changed = gstElemMake("volume")
        self.changed.set_property("volume", 0)
        audioconvert1 = gstElemMake("audioconvert") 
#=02 alsasink #1
#=04 sync=False - gstreamer bug
        sink0 = gstElemMake("alsasink") #02
        sink0.set_property("device", "plughw:0") #02
        sink0.set_property("sync", False) #04
        self.pipeline.add(mikesrc, tee, queue0, audioconvert0, 
                          self.pitch, self.chorus, self.changed, 
                          audioconvert1, sink0) 
        gst.element_link_many(mikesrc, tee, queue0, audioconvert0, 
                              self.pitch, self.chorus, self.changed, 
                              audioconvert1, sink0) 
        queue2 = gstElemMake("queue") 
        audioconvert2 = gstElemMake("audioconvert") 
        self.normal = gstElemMake("volume", "normal") 
        self.normal.set_property("volume", 0)
#=03 alsasink #2
        sink1 = gstElemMake("alsasink") #03
        sink1.set_property("device", "plughw:2") #03
        sink1.set_property("sync", False) #04
        self.pipeline.add(queue2, audioconvert2, self.normal, sink1) 
        gst.element_link_many(queue2, audioconvert2, self.normal, sink1) 
#=05 get_request_pad()
        tpad1 = tee.get_request_pad("src%d")  #05
        q2sink = queue2.get_pad("sink") 
        tpad1.link(q2sink) 
        self.pipeline.set_state(gst.STATE_PLAYING)
#--
    def on_normalScale_value_changed(self, w): #--
        scale = w.get_value() #--
        self.normal.set_property("volume", scale/100)#--
#--
    def on_changedScale_value_changed(self, w):#--
        scale = w.get_value() #--
        self.changed.set_property("volume", scale/100)#--
#--
    def on_pitchScale_value_changed(self, w):#--
        scale = w.get_value() #--
        # Scale is 0.25 -> 4#--
        if scale < 0: scale = 0.25 + (3+scale) * 0.25#--
        else: scale = 1 + scale#--
        self.pitch.set_property("Pitch-shift", scale)#--
#--
    def on_voiceScale_value_changed(self, w):#--
        scale = w.get_value() #--
        self.chorus.set_property("Number-of-voices", int(scale))#--
#--
    def on_quit_activate(self, widget):#--
        gtk.main_quit()#--
#--
if __name__ == "__main__":#--
    live=Live()#--
    gtk.main()#--
